import requests
import time
import sys
sys.path.append("../")
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
from frameworks.cores.LoadClass import *
from controller.ApiController import *
import json
import time

loader = LoadClass()

"""
#f2:最新价 f3:涨幅 f4:涨跌额 f5:成交量 f6成交额 f7:振幅
#f8:换手率 f9:市盈率 f10:量比 f11: f12:代码 f14:股票名称 f15:最高
#f16:最低 f17:今开 f18:昨收 f20:总市值 f21:流通市值
#f22:
"""

base = StockBaseInfo()
def getMiniteData(code,pre):
    time.sleep(0.1)
    dfcfCode = base.translateDfcfCode(code)
    response = requests.get("https://push2.eastmoney.com/api/qt/stock/trends2/get?secid="+dfcfCode+"&fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=e1e6871893c6386c5ff6967026016627&iscr=0&isqhquote=")
    data = response.json()
    i = 1
    score = 0
    for row in data["data"]["trends"]:
        line = row.split(",")
        close = float(line[2])
        money = line[6]
        avag = line[7]
        zf = (close-pre)/pre
        score += zf*i
        i += 1
    return [round(score,2),round(zf*100,2)]

codes = []
apiController = ApiController()
numlist = [1,2,3,9]
for num in numlist:
    prettyTable = PrettyTableUtil(["证券代码", "证券名称", "score", "涨幅"])
    newParam = {}
    newParam["num"] = num
    obj = apiController.getZhangTingPage(newParam)
    for line in obj:
        score,zf = getMiniteData(line["code"][2:],line["pre"])
        prettyTable.addRow([line["code"],line["codename"],score,zf])
    print(prettyTable.renderTable("score"))
    break

